package com.yikome.utils;

import java.util.List;

import com.google.common.collect.ImmutableList;
import com.itmuch.lightsecurity.jwt.User;
import com.itmuch.lightsecurity.jwt.UserOperator;

public final class SecurityUtils {

private static UserOperator userOperator ; 
	
	private SecurityUtils() {}
    
	private static void initSecurityUtils() {
		userOperator = SpringUtils.getBean(UserOperator.class);
	}
	
    public static String getCurrentUsername() {
    	
    	try {
    		User user = getCurrentUser();
        	
            return user.getUsername();
    	
    	}catch (Exception e) {
    		
			return "" ; 
		}
    }
    
    public static List<String> getCurrentRoles() {
    	
    	try {
    		User user = getCurrentUser();
        	
            return user.getRoles();
    	
    	}catch (Exception e) {
    		
			return ImmutableList.of() ; 
		}
    }
    
    public static User getCurrentUser() {
    	
    	if(userOperator == null) initSecurityUtils();
    	
    	User user = userOperator.getUser();
    	
        return user;
    }
}
